import cx_Oracle
import json

def process_wsfa_data():
    # 数据库连接配置
    connection = cx_Oracle.connect(
        user="your_username",
        password="your_password",
        dsn="your_connection_string"
    )
    
    try:
        cursor = connection.cursor()
        
        # 查询 wsfa 表中的数据
        query = """
            SELECT wsfa010, wsfa011
            FROM wsfa_file 
            WHERE wsfa011 IS NOT NULL
        """
        
        cursor.execute(query)
        results = []
        
        for row in cursor:
            try:
                # 解析 response 内容 (wsfa011)
                response_data = json.loads(row[1].read() if hasattr(row[1], 'read') else row[1])
                
                # 检查是否包含所需的数据结构
                if 'digi-body' in response_data and 'std_data' in response_data['digi-body']:
                    std_data = response_data['digi-body']['std_data']
                    
                    # 检查是否有 parameter
                    if 'parameter' in std_data:
                        parameter = std_data['parameter']
                        
                        # 提取 log_id 和 datakey
                        log_id = parameter.get('log_id')
                        datakey = parameter.get('datakey')
                        
                        # 检查是否是执行成功的记录
                        if parameter.get('description') == "执行成功":
                            results.append({
                                'log_id': log_id,
                                'datakey': datakey
                            })
            except json.JSONDecodeError:
                continue  # 跳过无法解析的 JSON
            except Exception as e:
                print(f"Error processing row: {e}")
                continue
        
        # 打印结果
        print("\n成功找到的记录:")
        print("=" * 50)
        for result in results:
            print(f"log_id: {result['log_id']}")
            print(f"datakey: {result['datakey']}")
            print("-" * 50)
        
        return results
        
    finally:
        cursor.close()
        connection.close()

if __name__ == "__main__":
    process_wsfa_data() 